<?php


class Home extends M_Controller
{

    public $field;

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 订单管理
     */
    public function index()
    {
        $this->_admin_list(L('订单管理'), 0);
    }

    /**
     * 待付款
     */
    public function fk()
    {
        $this->_admin_list(L('待付款的订单'), 1);
    }

    /**
     * 待发货
     */
    public function fh()
    {
        $this->_admin_list(L('待发货的订单'), 2);
    }

    /**
     * 交易完成
     */
    public function wc()
    {
        $this->_admin_list(L('交易完成的订单'), 3);
    }

    /**
     * 交易关闭
     */
    public function close()
    {
        $this->_admin_list(L('交易关闭的订单'), 9);
    }


    /**
     * 订单详情
     */
    public function info()
    {

        $id = (int)$this->input->get('id');
        $data = $this->models('Module/order')->get_info($id);
        if (!$data) {
            $this->admin_msg(L('订单不存在'), $_SERVER['HTTP_REFERER']);
        }
        $box = [];
        $this->load->library('dip');
        if ($data['mid'] == 'box' && $data['order_status'] == 3) {
            $box = $this->models('favorite')->cardInfo(
                ['oid' => $data['id']],
                'cid,catid,title,number,total,thumb,price'
            );
        }
        $this->template->assign(array(
            'id' => $id,
            'kd' => require LIBRARIES . 'Kd.php',
            'menu' => $this->get_menu_v3(array(
                L('订单管理') => array($this->_get_back_url('order/home/index'), 'reply'),
                L('订单详情') => array('order/admin/home/info/id/' . $id, 'file-text')
            )),
            'back' => ADMIN_URL . SELF . '?s=order&c=home&m=',
            'order' => $data,
            'field' => $this->get_cache('module-' . SITE_ID . '-order', 'field'),
            'paytype' => $this->mconfig['paytype'],
            'box' => $box
        ));
        $this->template->display(is_file(APPPATH . 'templates/admin/order_info_' . $data['mid'] . '.html') ? 'order_info_' . $data['mid'] . '.html' : 'order_info.html');
    }

    // 快递接口
    public function kd()
    {
        echo str_replace(
            array('<tr><th>时间</th><th>记录</th></tr>', 'ickd_return'),
            array('', 'table'),
            $this->_kd_info($_GET['name'], $_GET['sn'])
        );
        exit;
    }

    // 市场订单列表
    public function marketList()
    {
        $param = $this->input->post('data');
        $page = max($_GET['page'], 1);
        $pageSize = (int)$_GET['pageSize'] ?: 10;
        $parmaData = [
            'start' => $param['start'],
            'end' => $param['end'],
            'sn' => $param['sn'],
            'field' => $param['field'],
            'keyword' => $param['keyword']
        ];
        if ($parmaData['start'] && empty($param['end'])) {
            $parmaData['end'] = time();
        }
        $field = [
            ['fieldname' => 'sn', 'name' => '订单号'],
            ['fieldname' => 'buy_name', 'name' => '购买者姓名'],
            ['fieldname' => 'sell_name', 'name' => '出售者姓名'],
            ['fieldname' => 'title', 'name' => '名称']
        ];
        if ($param['action'] == 'del' && $param['ids']) {
            $ids = explode(',', $param['ids']);
            if ($this->models('market')->delMarketOrder($ids)) {
                $this->msg(1, '删除成功');
            } else {
                $this->msg(0, '操作失败');
            }
        }
        $result = $this->models('market')->marketOrderList(array_filter($parmaData), $page, $pageSize);
        $parmaData['total'] = $result['num'];
        $this->template->assign([
            'list' => $result['list'],
            'pages' => $this->get_pagination(dr_url('admin/admin/log', $parmaData), $result['num']),
            'param' => $parmaData,
            'field' => $field
        ]);
        $this->template->display('market_list.html');
    }

    // 订单详情
    public function marketInfo()
    {
        $id = $this->input->get('id');
        if ((int)$id < 1) {
            $this->msg(0, '参数错误');
        }
        $info = $this->models('market')->marketOrderInfo(
            $id,
            'id,sn,out_trade_no,buy_uid,buy_name,sell_uid,sell_name,price,num,total,title,thumb,order_time,order_status
            ,pay_status,pay_time,pay_type,team_code'
        );
        $this->template->assign([
            'order' => $info
        ]);
        $this->template->display('market_info.html');
    }
}